package global_config

import (
	"github.com/astaxie/beego/orm"
	. "haproxy-mgr-go/models"
	"fmt"
)

func FindAll() []GlobalConfig {
	var globalconfigs []GlobalConfig
	orm.NewOrm().QueryTable("global_config").All(&globalconfigs)

	return globalconfigs
}

func InitConfigs() GlobalConfig {

	globalConfig := GlobalConfig{
		GlobalChroot:          "/var/lib/haproxy",
		GlobalLog:             "127.0.0.1 local0",
		GlobalMaxconn:         "10000",
		GlobalNbproc:          "1",
		GlobalUser:            "haproxy",
		GlobalGroup:           "haproxy",
		GlobalDaemon:          "1",
		GlobalPidfile:         "/var/run/haproxy.pid",
		DefaultMod:            "tcp",
		DefaultRetries:        "3",
		DefaultConnectTimeout: "10s",
		DefaultClientTimeout:  "60s",
		DefaultServerTimeout:  "60s",
		DefaultCheckTimeout:   "10s",
	}

	if n, err := orm.NewOrm().InsertOrUpdate(&globalConfig); err != nil {
		fmt.Println("init data error")
	} else {
		fmt.Printf("init data sucess, insert %d data", n)
	}

	return globalConfig
}
